// JavaScript Document
var share = {};
share.get_api_url = function () {
    var domain = window.location.origin;
    var api_url;
    if (domain.indexOf('https://') > -1) {
        api_url = 'https://api.qjbuluo.com/api/'
    } else {
        api_url = 'http://api.qjbuluo.com/api/'
    }
    return api_url;
}
share.ajax_control = function (obj) {
    $.ajax({
        type: typeof (obj.type) != 'undefined' ? obj.type : "GET",
        url: obj.url,
        data: obj.data ? obj.data : '',
        dataType: typeof (obj.dataType) != 'undefined' ? obj.dataType : "json",
        success: function (data) {
            obj.success ? obj.success(data) : console.log(data);
        },
        error: function (error) {
            obj.error ? obj.error(error) : console.log('error', error);
        },
        complete: function (xhr) {
            obj.complete ? obj.complete(xhr) : console.log('complete', xhr);
        }
    });
}

share.add_tip_a = function (dom, msg) {
    dom.append('<div class="no_log_tip">' + msg + '</div>')
}

share.remove_tip = function () {
    $('.no_log_tip').length > 0 ? $('.no_log_tip').remove() : '';
}

share.get_param = function (paramname) {
    var reg = new RegExp("(^|&)" + paramname + "=([^&]*)(&|$)");
    var s = window.location.search.substr(1).match(reg);
    if (s != null) {
        return unescape(s[2]);
    }
    return null;
}

share.get_video_info = function (url) {
    var obj = {
        size: 0,
        time: 0
    }
    if (url.indexOf('?') < 0 || url.indexOf('&') < 0)
        return obj
    var sizeArr = url.split('?')[1].split('&');
    for (var i = 0; i < sizeArr.length; i++) {
        if (sizeArr[i].indexOf('_t=') != -1) {
            obj.time = sizeArr[i].replace('_t=', '')
        }
        if (sizeArr[i].indexOf('_s=') != -1) {
            obj.size = size_conver(sizeArr[i].replace('_s=', '') * 1024)
        }
    }
    return obj;
}

function size_conver(limit) {
    var size = "";
    if (limit < 0.1 * 1024) { //如果小于0.1KB转化成B
        size = limit.toFixed(2) + "B";
    } else if (limit < 0.1 * 1024 * 1024) {//如果小于0.1MB转化成KB
        size = (limit / 1024).toFixed(2) + "KB";
    } else if (limit < 0.1 * 1024 * 1024 * 1024) { //如果小于0.1GB转化成MB
        size = (limit / (1024 * 1024)).toFixed(2) + "MB";
    } else { //其他转化成GB
        size = (limit / (1024 * 1024 * 1024)).toFixed(2) + "GB";
    }

    var sizestr = size + "";
    var len = sizestr.indexOf("\.");
    var dec = sizestr.substr(len + 1, 2);
    if (dec == "00") {//当小数点后为00时 去掉小数部分
        return sizestr.substring(0, len) + sizestr.substr(len + 3, 2);
    }
    return sizestr;
}

share.get_img_info = function (url) {
    var obj = {
        w: 0,
        h: 0
    }
    if (url.indexOf('?') < 0 || url.indexOf('&') < 0)
        return obj
    var sizeArr = url.split('?')[1].split('&');
    for (var i = 0; i < sizeArr.length; i++) {
        if (sizeArr[i].indexOf('_w=') != -1) {
            obj.w = sizeArr[i].replace('_w=', '')
        }
        if (sizeArr[i].indexOf('_h=') != -1) {
            obj.h = sizeArr[i].replace('_h=', '')
        }
    }
    return obj;
}

share.fomat_num = function (val) {
    let obj = {
        num: val,
        unit: ''
    }
    if (parseInt(val) > 9999) {
        obj.num = val / 10000;
        obj.unit = 'w';
    }
    if (parseInt(val) > 999) {
        obj.num = val / 1000;
        obj.unit = 'k';
    }
    return obj
}

share.get_age = function (str) {
    if (str == undefined) return 1
    var birthdayArr = str.substr(0, 10).split('-');
    var myDate = new Date();
    var year = myDate.getFullYear();
    var mon = myDate.getMonth() + 1;
    var day = myDate.getDate();
    if (parseInt(mon) > parseInt(birthdayArr[1]) || (parseInt(mon) == parseInt(birthdayArr[1]) && parseInt(day) >= parseInt(birthdayArr[2]))) {
        return year - parseInt(birthdayArr[0])
    } else {
        return year - parseInt(birthdayArr[0]) - 1
    }
}

share.get_sex = function (val) {
    let sexstr = '';
    if (val == undefined)
        val = 'unknown'
    switch (val.toLowerCase()) {
        case 'male':
            sexstr = '男'
            break;
        case 'female':
            sexstr = '女'
            break;
        case 'unknown':
            sexstr = '未知'
            break;
    }
    return sexstr
}

share.creat_map = function (domid, longitude, latitude) {
    //创建地图
    var map = new BMap.Map(domid);
    var point = new BMap.Point(longitude, latitude); //longitude-经度  latitude-纬度
    map.centerAndZoom(point, 15);
    var zoomCtrl = new BMap.ZoomControl();
    var scaleCtrl = new BMap.ScaleControl();
    map.addControl(zoomCtrl);
    map.addControl(scaleCtrl);
    var marker = new BMap.Marker(point);        // 创建标注    
    map.addOverlay(marker);                     // 将标注添加到地图中 
}

share.isIos = function () {
    if (/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)) {
        return true;
    }
}

share.isAndroid = function () {
    var userAgent = navigator.userAgent;
    return (userAgent.indexOf('Android') > -1 || userAgent.indexOf('Adr') > -1 || userAgent.indexOf('Linux') > -1);
}

share.download = function (androidUrl, IosUrl) {
    var _androidUrl = ""
    var _IosUrl = ""
    var ua = navigator.userAgent.toLowerCase();
    if (ua.match(/MicroMessenger/i) == "micromessenger") {//qq浏览器
        if (share.isAndroid()) {//andrio打开应用宝
            window.open('https://app.qjbuluo.com/qqstore.html')
        }
        if (share.isIos()) {
            window.open('https://itunes.apple.com/cn/app/%E5%B7%A7%E5%8C%A0%E9%83%A8%E8%90%BD/id1462605736?mt=8')
        }
    } else {
        if (share.isAndroid()) {
            window.open(androidUrl || _androidUrl)
        }
        if (share.isIos()) {
            window.open('https://itunes.apple.com/cn/app/%E5%B7%A7%E5%8C%A0%E9%83%A8%E8%90%BD/id1462605736?mt=8')
        }
    }
}


